<?php
include_once ('../../php/postgre.php');
session_start();
$act = '';
if (isset($_POST['act']))
    $act = $_POST['act'];
if ($act == 'kegiatan_action') {
    $id_kegiatan = $_POST['id_kegiatan'];
	$id_indikator = $_POST['id_indikator'];
	$kode_kegiatan = $_POST['kode_kegiatan'];
    $nama_kegiatan = $_POST['nama_kegiatan'];

    if (!is_numeric($id_kegiatan) || !is_numeric($id_indikator) || empty($kode_kegiatan) || empty($nama_kegiatan)) {
        echo "Err : invalid input.";
        exit;
    }

    if ($id_kegiatan > 0) {
        exec_query("update spm_kegiatan set id_indikator = ".$id_indikator.",kode_kegiatan = '".$kode_kegiatan."',nama_kegiatan = '".$nama_kegiatan."' where id_kegiatan = ".$id_kegiatan);
        echo 'success__';
    } else {
        $maxid = get_maxid('id_kegiatan', 'spm_kegiatan');
        exec_query("insert into spm_kegiatan (id_kegiatan, id_indikator, kode_kegiatan, nama_kegiatan) values 
					(" . $maxid . "," . $id_indikator . ", '" . $kode_kegiatan . "', '" . $nama_kegiatan."' )");
        $st = get_data('select id_kegiatan from spm_kegiatan where id_kegiatan=' . $maxid);
        if ($st['id_kegiatan'] == $maxid) {
            echo 'success__';
            $id_kegiatan = $maxid;
        }
    }

	echo $id_kegiatan.'__';
	view_kegiatan();
}else if($act == 'del_kegiatan'){
	$id_kegiatan = $_POST['id_kegiatan'];
	if(is_numeric($id_kegiatan)){
		exec_query("DELETE FROM spm_kegiatan WHERE id_kegiatan =".$id_kegiatan);
		echo 'success__';
	}else{
		echo 'Error';
	}
	view_kegiatan();
} else if ($act == 'edit_kegiatan') {
    $id_kegiatan = $_POST['id_kegiatan'];
    if (is_numeric($id_kegiatan)) {
        $data = get_data("select id_kegiatan,id_indikator, kode_kegiatan, nama_kegiatan from spm_kegiatan where id_kegiatan =" . $id_kegiatan);
        $ress = implode($data, '__');
        print_r($ress);
        echo "__";
    } else {
        echo 'error';
    }
    view_kegiatan();
}

function view_kegiatan() {
				$no=1;
					$keg = get_datas("SELECT * FROM spm_indikator i,spm_kegiatan k where i.id_skpd=".$_SESSION['_id']." AND i.id_indikator=k.id_indikator");
					foreach ($keg as $keg){
					?>
						<tr>
							<td><?php echo $no ?></td>
							<td><?php echo $keg['kode_indikator'];?></td>
							<td><?php echo $keg['nama_indikator']?></td>
							<td><?php echo $keg['kode_kegiatan']?></td>
							<td><?php echo $keg['nama_kegiatan']?></td>
							<td style="width:200px; text-align:center;">
							<a class="btn btn-danger bt-delete-kegiatan" name="<?php echo $keg['id_kegiatan']; ?>">Delete</a>
                                <a  href="#modalwin" data-toggle="modal"  class="btn btn-info bt-edit-kegiatan" name="<?php echo $keg['id_kegiatan']; ?>">Edit</a>
							</td>
						</tr>
						<?php 
						$no++;
						}
						}
						?>



